# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.cardDao import CardDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertCard', methods=['POST'])
def insert_cardNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'userId': request.json.get('userId', ''),
        'cardName': request.json.get('cardName', ''),
        'cardPhone': request.json.get('cardPhone', ''),
        'cardType': request.json.get('cardType', ''),
        'cardCode': request.json.get('cardCode', '')
    }

    info = CardDao().insertCard(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findCardById', methods=['POST'])
def find_card_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    cardId = request.json.get('cardId', '')
    info = CardDao().findCardById(cardId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 1, "status": "error"}})


@app.route('/findCardByCondition', methods=['POST'])
def find_card_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    userId = request.json.get('userId', '')
    info = CardDao().findCardByConditon(userId=userId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 1, "status": "error"}})


@app.route('/deleteCardByIds', methods=['POST'])
def delete_card_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = CardDao().deleteCard(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateCardById', methods=['POST'])
def update_card_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        cardId = request.json['cardId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = CardDao().selectCard(cardId)
    info = {
        'cardId': request.json['cardId'],
        'userId': request.json.get('userId', oldInfo[1]),
        'cardName': request.json.get('cardName', oldInfo[2]),
        'cardPhone': request.json.get('cardPhone', oldInfo[3]),
        'cardType': request.json.get('cardType', oldInfo[4]),
        'cardCode': request.json.get('cardCode', oldInfo[5])
    }

    info = CardDao().updateCard(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
